package ml.kupin.sso.service.impl;

import ml.kupin.common.constant.KuPinConstant;
import ml.kupin.common.utils.KuPinResult;
import ml.kupin.mapper.TbUserMapper;
import ml.kupin.pojo.TbUser;
import ml.kupin.pojo.TbUserExample;
import ml.kupin.sso.service.RegisterService;
import ml.kupin.sso.utils.EncryptUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户注册Service实现类
 * <p>
 * Created by 小柯(Allen Kerr) on 17.5.6 11:04.
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private TbUserMapper userMapper;

    private Logger logger = LoggerFactory.getLogger(RegisterServiceImpl.class);

    /**
     * 检查用户名、电话、邮箱是否被占用，
     * 返回值为KupinResult，包含一个boolean类型的值
     *
     * @param param
     * @param type
     * @return
     */
    @Override
    public KuPinResult checkData(String param, Integer type) {
        //创建查询条件
        TbUserExample userExample = new TbUserExample();
        TbUserExample.Criteria criteria = userExample.createCriteria();
        //添加查询条件
        if (KuPinConstant.CheckRegisterData_username == type) {
            criteria.andUsernameEqualTo(param);
        } else if (KuPinConstant.CheckRegisterData_phone == type) {
            criteria.andPhoneEqualTo(param);
        } else if (KuPinConstant.CheckRegisterData_email == type) {
            criteria.andEmailEqualTo(param);
        } else {
            //检查类型错误，不继续往下走了
            return KuPinResult.notOk(false);
        }
        //执行查询操作
        List<TbUser> userList = userMapper.selectByExample(userExample);
        //判断
        if (userList != null && userList.size() > 0) {
            //被占用，返回false
            return KuPinResult.notOk(false);
        }
        //未被占用，返回true
        return KuPinResult.ok(true);
    }

    @Override
    public KuPinResult createUser(TbUser user) throws Exception {
        try {
            //1. 再次检查数据
            //1.1 非空性检查
            if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())
                    || StringUtils.isBlank(user.getPhone())) {
                return KuPinResult.notOk("注册信息不完整，请重新填写！");
            }
            //1.2 占用情况检查
            KuPinResult result = checkData(user.getUsername(), KuPinConstant.CheckRegisterData_username);
            if (!(boolean) result.getData()) {
                return KuPinResult.notOk("用户名已被占用，请重新填写！");
            }
            result = checkData(user.getPhone(), KuPinConstant.CheckRegisterData_phone);
            if (!(boolean) result.getData()) {
                return KuPinResult.notOk("手机号已被注册，，请重新填写！");
            }
            //2. 补全属性
            //2.1 密码
            user.setPassword(EncryptUtils.encrypt(user.getPassword()));
            //2.2 时间
            Date addAndUpdateTime = new Date();
            user.setCreated(addAndUpdateTime);
            user.setUpdated(addAndUpdateTime);
            //3. 执行插入操作
            userMapper.insertSelective(user);
        } catch (Exception e) {
            logger.error("注册服务发生异常！", e);
            e.printStackTrace();
            throw e;
        }
        //4. 返回注册成功
        logger.info(user.getUsername() + " 注册成功！");
        return KuPinResult.ok();
    }
}
